<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button
          type="default"
          @click="handleArea({})"
          v-auth="'proj:info:pmProjectinformation:edit'"
        >
          <Icon icon="ant-design:setting-outlined" /> {{ t('分配到所属区域') }}
        </a-button> -->

        <a-popconfirm
          title="是否确认分配到所属区域？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleArea({})"
        >
          <a-button type="default" v-auth="'btn:feipeidaosuoshuquyu'">
            <Icon icon="ant-design:setting-outlined" /> {{ t('分配到所属区域') }}
          </a-button>
        </a-popconfirm>

        <a-popconfirm
          title="是否确认取消分配？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleCancelFP()"
        >
          <a-button type="danger" v-auth="'btn:feipeidaosuoshuquyu'">
            <Icon icon="ant-design:redo-outlined" /> {{ t('取消分配') }}
          </a-button>
        </a-popconfirm>
      </template>
      <!-- <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.projectno }}
        </a>
      </template> -->

      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.projectno }}
        </a>
        
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProjInfoPmProjectinformationList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { taskAreaListData, setArea, cancelArea } from '/@/api/proj/info/pmProjectinformation';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';

  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('proj.info.pmProjectinformation');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('项目信息管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('项目编号'),
        field: 'projectno',
        component: 'Input',
        // defaultValue: userStore.getProjecte.code
      },
      {
        label: t('项目名称'),
        field: 'projectname',
        component: 'Input',
      },
      {
        label: t('所属区域'),
        field: 'region',
        component: 'Input',
      },
      {
        label: t('联系人'),
        field: 'contacts',
        component: 'Input',
      },
      {
        label: t('创建时间'),
        field: 'createtime',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('显示全部项目'),
        field: 'ope',
        component: 'Checkbox',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('项目编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'projectno',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.projectname',
      sorter: true,
      width: 300,
      align: 'left',
    },
    {
      title: t('所属区域'),
      dataIndex: 'region',
      key: 'a.region',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('联系人'),
      dataIndex: 'contacts',
      key: 'a.contacts',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('创建时间'),
      dataIndex: 'createtime',
      key: 'a.createtime',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑项目信息'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'proj:info:pmProjectinformation:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: taskAreaListData,
    beforeFetch: (params) => {
      if (params.ope) {
        params.ope = '1';
      }
      return params;
    },
    columns: tableColumns,
    // actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        // projectno: code,
        id: record.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleSave(record: Recordable) {
    let data = {
      projectCode: record.projectno,
      projectName: record.projectname,
    };
    const res = await userCollectSave(data);
    showMessage(res.message);
    handleSuccess();

    let res1 = await userCollectListDataNoPage();
    await userStore.setProjecteList(res1);
  }

  async function handleCancel(record: Recordable) {
    let data = {
      projectCode: record.projectno,
      // projectName:record.projectname
    };
    const res = await userCollectCancel(data);
    showMessage(res.message);
    handleSuccess();

    let res1 = await userCollectListDataNoPage();
    // console.log(res1);
    await userStore.setProjecteList(res1);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmProjectinformationDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  onMounted(async () => {
    handleProjecte();
  });

  async function handleProjecte() {
    await getForm().setFieldsValue({
      projectno: router.currentRoute.value.query.projectno
        ? router.currentRoute.value.query.projectno
        : userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      projectno: router.currentRoute.value.query.projectno
        ? router.currentRoute.value.query.projectno
        : userStore.getProjecte.code,
    });
  }

  // 分配区域
  async function handleArea() {
    if (getSelectRows().length) {
      let selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await setArea({ selIds: selIds.value });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！');
    }
  }

  // 取消分配区域
  async function handleCancelFP() {
    if (getSelectRows().length) {
      let selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await cancelArea({ selIds: selIds.value });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请选择一行数据！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
